package com.lnlyj.navigation.demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lnlyj.navigation.LnlyjNavigationBar;
import com.lnlyj.navigation.LnlyjNavigationPageAdapter;

/**
 * Created by Wanglei on 2019/11/26.
 */

public class ViewPagerActivity extends AppCompatActivity {

    ViewPager viewPager;
    TestAdapter adapter;
    LnlyjNavigationBar bar;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);

        viewPager = findViewById(R.id.view_pager);
        adapter = new TestAdapter(getSupportFragmentManager());

        bar = findViewById(R.id.nav_bar);
        bar.setupWithViewPager(viewPager, true);

        bar.setTabSelectedColor(getResources().getColor(R.color.colorPrimary));
        bar.setTabDefaultColor(getResources().getColor(R.color.colorAccent));

        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(3);
    }

    @SuppressLint("ValidFragment")
    public static class TestFragment extends Fragment {

        private int index;

        public TestFragment(int index) {
            super();
            this.index = index;
        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View v = inflater.inflate(android.R.layout.simple_list_item_1, null);
            TextView tv = v.findViewById(android.R.id.text1);
            tv.setText("页面" + (index + 1));
            return v;
        }
    }

    class TestAdapter extends FragmentPagerAdapter implements LnlyjNavigationPageAdapter {


        public TestAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return new TestFragment(position);
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return "页面" + (position + 1);
        }

        @Override
        public CharSequence getTabTitle(int position) {
            return "Tab" + (position + 1);
        }
        
        @Override
        public int getTabDrawable(int position) {
            return R.drawable.ic_directions_bike_black;
        }
    }
}
